﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Diagnostics;

namespace MyRTS
{
    public class NewScene
    {
      //  public List<Node> Definisions = new List<Node>();
        public int Objectscount = 0;

        public struct Definitions
        {
            public string name;
            public int qty;
            public Definitions(string s, int i)
            {
                name = s;
                qty = i;
            }
        }
        public List<Definitions> def = new List<Definitions>();
        public List<Node> ModelsNodeList = new List<Node>();
        public List<Node> MaterialNodeList = new List<Node>();
        public List<Node> GlobalSettingslNodeList = new List<Node>();
        public List<Node> ConnectionList = new List<Node>();

        public List<NewModel> NewModelList = new List<NewModel>();

        int shadowprogram;

        public NewScene(fbx f, int shaderProgramHandle)
        {
            foreach (Node n in f.NodeList)
            {
                shadowprogram = shaderProgramHandle;
                if (n.name == "Definitions")
                {
                    foreach (Node m in n.NodeList)
                    {
                        if (m.name == "Count")
                        {
                            int pl = 0;
                            Objectscount = Convert.ToInt32(m.PropertyList[0].Datas);
                        }
                        if (m.name == "ObjectType")
                        {
                            string type = m.PropertyList[0].Datas;
                            foreach (Node o in m.NodeList)
                            {
                                if (o.name == "Count")
                                {
                                    int h = Convert.ToInt32(o.PropertyList[0].Datas);
                                    Definitions l = new Definitions(type, h);
                                    def.Add(l);
                                }
                            }
                         
                           
                        }
                    }
                }
                if (n.name == "Objects")
                {
                    foreach (Node m in n.NodeList)
                    {
                        foreach (Definitions k in def)
                        {
                            if (k.name == m.name)
                            {
                                if (m.name == "Model")
                                {
                                    ModelsNodeList.Add(m);
                                }
                                if (m.name == "Material")
                                {
                                    MaterialNodeList.Add(m);
                                }
                                if (m.name == "GlobalSettings")
                                {
                                    GlobalSettingslNodeList.Add(m);
                                }
                                



                            }
                        }
                    }

                    
                }
                if (n.name == "Connections")
                {
                    ConnectionList.Add(n);
                }
                int y = 0;




            }
            if (def.Count != Objectscount)
            {
               // MessageBox.Show("Wrong object count");
            }

            ProcessMolels(ModelsNodeList);


        }

        private void ProcessMolels(List<Node> lista)
        {
            foreach (Node n in lista)
            {

                if (n.PropertyList[1].Datas.Contains("Mesh"))
                {
                    NewModel m = new NewModel(n, shadowprogram);
                    NewModelList.Add(m);
                }
            }

        }

    }

}